package com.sixseven.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sixseven.bean.Cowry;
import com.sixseven.mapper.CowryMapper;
import com.sixseven.repository.CowryRepository;
import com.sixseven.service.CowryService;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Belly
 * @version 1.1.0
 */
@Service
public class CowryServiceImpl extends ServiceImpl<CowryMapper, Cowry> implements CowryService {
    @Resource
    private CowryMapper cowryMapper;

    @Resource
    private CowryRepository cowryRepository;

    @Resource
    private ElasticsearchRestTemplate template;

    @Override
    public List<Cowry> queryCowryList() {
        List<Cowry> cowryList = cowryMapper.queryCowryList();
        if (cowryList == null){
            return null;
        }
        return cowryList;
    }

    @Override
    public List<Cowry> queryCowryListByStatus(Integer status) {
        if (status == null){
            return null;
        }
        LambdaQueryWrapper<Cowry> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
        objectLambdaQueryWrapper.eq(Cowry::getStatus, status);
        List<Cowry> cowryList = cowryMapper.selectList(objectLambdaQueryWrapper);
        return cowryList;
    }

    //自动补齐
    @Override
    public List<String> autoSuggest(String keyword) {

        //1.创建补齐请求
        SuggestBuilder suggestBuilder = new SuggestBuilder();

        //2.构建补齐条件
        CompletionSuggestionBuilder suggestionBuilder = SuggestBuilders
                .completionSuggestion("name") //需要补齐的字段
                .prefix(keyword) //补齐的前缀
                .skipDuplicates(true) //去掉重复的关键词
                .size(10); //最多补齐的数
        suggestBuilder.addSuggestion("prefix_suggestion", suggestionBuilder);//放入补齐请求中

        //3.发送请求
        SearchResponse response = template.suggest(suggestBuilder, IndexCoordinates.of("name"));

        //4.处理结果
        List<String> result = response.getSuggest()
                .getSuggestion("prefix_suggestion")
                .getEntries()
                .get(0)
                .getOptions()
                .stream()
                .map(Suggest.Suggestion.Entry.Option::getText)
                .map(Text::toString).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<com.sixseven.document.Cowry> highLightSearch(String keyword) {
        List<SearchHit<com.sixseven.document.Cowry>> result =
                cowryRepository.findByNameMatches(keyword);

        //处理结果，封装为com.sixseven.document.Cowry类型的集合
        List<com.sixseven.document.Cowry> cowries = new ArrayList<>();
        for (SearchHit<com.sixseven.document.Cowry> cowrySearchHit : result) {
            com.sixseven.document.Cowry content = cowrySearchHit.getContent();
            Map<String, List<String>> highlightFields = cowrySearchHit.getHighlightFields();
            if (highlightFields.get("name") != null){
                content.setName(highlightFields.get("name").get(0));
            }
            cowries.add(content);
        }
        return cowries;
    }
}
